package com.ittx.dzt.baidu.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.baidu.handler.AbstractBaseBaiduApiHandler;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询省、市、区/县、社区/街道列表
 * <p>
 * 查询区域地址（查询省、市、区/县、社区/街道列表） /PlatRegionStoreService/getAreaList
 * cy.area.download.splitting
 * https://dev2.baidu.com/content?sceneType=0&pageId=104766&nodeId=1539&subhead=
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/2/27
 */
@Slf4j
@Service
public class BaiduGetAreaListService extends AbstractBaseBaiduApiHandler<Map> {
	@Override
	public String getLocalApiMethod() {
		return "/PlatRegionStoreService/getAreaList";
	}

	@Override
	public String getBaiduMethod() {
		return "/PlatRegionStoreService/getAreaList";
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, Map reqMap) {
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		try {
			Map<String, Object> extendMap = platformApiParamDTO.getExtendMap();
			String authUserName = MapUtil.getStr(extendMap, "authUserName", "");
			if (StrUtil.isEmpty(authUserName)) {
				return Result.error("授权用户名,不可为空,请在拓展参数中加入参数:{}", "authUserName");
			}
			Map<String, Object> headerMap = MapUtil.newHashMap();
			headerMap.put("accessToken", storeConfig.getSessionKey());
			headerMap.put("userName", authUserName);

			Map<String, String> reqParamMap = new HashMap<>();
			if (StrUtil.isNotEmpty(MapUtil.getStr(reqMap, "provinceCode", ""))) {
				reqParamMap.put("provinceCode", MapUtil.getStr(reqMap, "provinceCode", ""));
			}
			if (StrUtil.isNotEmpty(MapUtil.getStr(reqMap, "cityCode", ""))) {
				reqParamMap.put("cityCode", MapUtil.getStr(reqMap, "cityCode", ""));
			}
			if (StrUtil.isNotEmpty(MapUtil.getStr(reqMap, "countyCode", ""))) {
				reqParamMap.put("countyCode", MapUtil.getStr(reqMap, "countyCode", ""));
			}
			if (StrUtil.isNotEmpty(MapUtil.getStr(reqMap, "querySubNodeFlag", ""))) {
				reqParamMap.put("querySubNodeFlag", MapUtil.getStr(reqMap, "querySubNodeFlag", ""));
			}

			Map<String, Object> reqPostMap = new HashMap<>();
			reqPostMap.put("header", headerMap);
			reqPostMap.put("body", reqParamMap);

			Result<?> result = this.executeApi(storeConfig, reqPostMap, platformApiParamDTO.getRequestId());
			if (result.hasError()) {
				return result;
			}
			Map areaMap = (Map) result.getData();
			Map bodyMap = MapUtil.get(areaMap, "body", Map.class);
			List data = MapUtil.get(bodyMap, "data", List.class);
			return Result.success(data);
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), e.getMessage());
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg));
		}
	}


}
